﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Prodma.DataAccess.DataService;
using Prodma.DataAccess.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Database;
using System.Data;
using Prodma.DataAccess.Models.UserControlViewModels;

namespace Prodma.Panel.Controllers
{
    public class SepetController : Controller
    {

        #region Repository Setters

        private SepetService service = new SepetService();

        public SepetController()
        {
        }

        #endregion

        #region Index()

        public ActionResult Index(int? urunId)
        {
            LoginUser user =  (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            IList<SepetVm> sepetim = service.GetList(user.Id);
            System.Web.HttpContext.Current.Session["chart"] = sepetim;
            if (sepetim.Count != 0)
            {
                return View(sepetim);
            }
            else
            {
                return RedirectToAction("EmptyChart", "Sepet");
            }
        }

        #endregion


        public ActionResult EmptyChart()
        {
            return View();
        }

        public ActionResult AddSiparisNot()
        {
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            string aciklama = Request.Form["siparisNot"];
            tb_Sepet spt;

            foreach (SepetVm urun in sepet)
            {
                spt = new tb_Sepet();
                spt.Aciklama = aciklama;
                service.AddSiparisNot(urun.ID, spt);
            }

            return RedirectToAction("CompleteChart", "Sepet");
        }
        public ActionResult Offer()
        {
            LoginUser user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            decimal karMarji = (1/10)+1;
            IList<SepetVm> sepetim = service.GetOffer(user.Id, karMarji);
            if (sepetim.Count != 0)
            {
                return View(sepetim);
            }
            else
            {
                return RedirectToAction("EmptyChart", "Sepet");
            }
        }

        public static ChartUserControlVm GetChartInformation()
        {
            IList<SepetVm> sepetim = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            ChartUserControlVm chartInformation = new ChartUserControlVm();
            foreach (SepetVm urun in sepetim)
            {
                chartInformation.PartsTotal = chartInformation.PartsTotal + urun.Miktar;
                chartInformation.PriceTotal = chartInformation.PriceTotal + (urun.UrunID.Fiyat ?? 0 * urun.Miktar);
            }

            chartInformation.Currency = "TL";

            return chartInformation;
        }

        #region AddChart

        public bool ControlChart()
        {
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            int urunId = Convert.ToInt32(Request.Form["UrunId"]);
            if (sepet.Count(urn => urn.UrunID.ID == urunId) > 0)
                return false;
            else
                return true;
        }
        public ActionResult AddChart(int id)
        {
            var urunler = (IList<UrunlerVm>)System.Web.HttpContext.Current.Session["Urunler"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            int quantity = Convert.ToInt32(Request.Form["Text" + id.ToString()]);
            int productId = Convert.ToInt32(Request.Form["Product" + id.ToString()]);

            tb_Sepet spt;
            spt = new tb_Sepet();
            if (sepet.Count(urn => urn.UrunID.ID == productId) == 0)
            {
                spt.Miktar = quantity;
                spt.UserID = user.Id;
                spt.UrunID = productId;
                //spt.tb_UserReference.EntityKey = new EntityKey("DataContext.tb_User", "ID", user.Id);
                //spt.STOKReference.EntityKey = new EntityKey("DataContext.STOK", "ID", productId);
                spt.AktifPasif = true;
                spt.OnayTarih = DateTime.Now;
                service.New(spt);
            }
            else
            {
                service.AddChart(user.Id, productId, quantity);
            }
                       

            return RedirectToAction("Index", "Sepet");
        }

        public ActionResult AddChartDynamic()
        {
            var urunler = (IList<UrunlerVm>)System.Web.HttpContext.Current.Session["Urunler"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            SepetService sepetService = new SepetService();


            int id = Convert.ToInt32(Request.Form["id"]);
            int quantity = Convert.ToInt32(Request.Form["quantity"]);

            tb_Sepet spt;
            spt = new tb_Sepet();
            if (sepet.Count(urn => urn.UrunID.ID == id) == 0)
            {
                spt.Miktar = quantity;
                spt.UserID = user.Id;
                spt.UrunID = id;
                spt.AktifPasif = true;
                spt.OnayTarih = DateTime.Now;
                service.New(spt);
            }
            else
            {
                service.AddChart(user.Id, id, quantity);
            }

            return PartialView("/views/shared/userControls/SepetList.ascx",sepetService.GetList(user.Id));
        }

        public ActionResult AddChartSingle(int id)
        {
            var urunler = (IList<UrunlerVm>)System.Web.HttpContext.Current.Session["Urunler"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            int quantity = Convert.ToInt32(Request.Form["quantity"]);

            tb_Sepet spt;
            spt = new tb_Sepet();
            if (sepet.Count(urn => urn.UrunID.ID == id) == 0)
            {
                spt.Miktar = quantity;
                spt.tb_UserReference.EntityKey = new EntityKey("DataContext.tb_User", "ID", user.Id);
                spt.STOKReference.EntityKey = new EntityKey("DataContext.STOK", "ID", id);
                spt.AktifPasif = true;
                spt.OnayTarih = DateTime.Now;
                service.New(spt);
            }
            else
            {
                service.AddChart(user.Id, id, quantity);
            }


            return RedirectToAction("Index", "Sepet");
        }
        public ActionResult AddChartTotal()
        {
            var urunler = (IList<UrunlerVm>)System.Web.HttpContext.Current.Session["Urunler"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            tb_Sepet spt;
            Dictionary<int, int> products = new Dictionary<int, int>();
            int rowCount = Convert.ToInt32(Request.Form["rowCount"]);


            for (int i = 1; i <= rowCount; i++)
            {
                int quantity = Convert.ToInt32( Request.Form["Text" + i.ToString()]);
                int productId = Convert.ToInt32(Request.Form["Product" + i.ToString()]);
                spt = new tb_Sepet();

                if (quantity != 0)
                {
                    if (sepet.Count(urn => urn.UrunID.ID == productId) == 0)
                    {
                        spt.Miktar = quantity;
                        spt.tb_UserReference.EntityKey = new EntityKey("DataContext.tb_User", "ID", user.Id);
                        spt.STOKReference.EntityKey = new EntityKey("DataContext.STOK", "ID", productId);
                        spt.AktifPasif = true;
                        spt.OnayTarih = DateTime.Now;
                        service.New(spt);
                    }
                    else
                    {
                        service.AddChart(user.Id, productId, quantity);
                    }
                }

            }            

            return RedirectToAction("Index","Sepet");
        }

        public ActionResult OrderChartDynamic()
        {
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            SepetService sepetService = new SepetService();

            int id = Convert.ToInt32(Request.Form["id"]);
            int quantity = Convert.ToInt32(Request.Form["quantity"]);
            int chartId = Convert.ToInt32(Request.Form["chartId"]);

            tb_Sepet spt;
            spt = new tb_Sepet();

            spt.Miktar = quantity;

            service.Update(chartId, spt);

            return PartialView("/views/shared/userControls/SepetList.ascx", sepetService.GetList(user.Id));

        }

        public ActionResult OrderChart()
        {
            var urunler = (IList<UrunlerVm>)System.Web.HttpContext.Current.Session["Urunler"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];

            tb_Sepet spt;
            Dictionary<int, int> products = new Dictionary<int, int>();



            for (int i = 1; i <= service.GetTotalRowCount(); i++)
            {
                int quantity = Convert.ToInt32(Request.Form["Text" + i.ToString()]);
                int productId = Convert.ToInt32(Request.Form["Product" + i.ToString()]);
                int chartId = Convert.ToInt32(Request.Form["Chart" + i.ToString()]);
                spt = new tb_Sepet();
                spt.Miktar = quantity;
                spt.tb_UserReference.EntityKey = new EntityKey("DataContext.tb_User", "ID", user.Id);
                spt.STOKReference.EntityKey = new EntityKey("DataContext.STOK", "ID", productId);
                spt.AktifPasif = true;
                spt.OnayTarih = DateTime.Now;

                service.Update(chartId, spt);

            }

            return RedirectToAction("Index", "Sepet");
        }

        public ActionResult DeleteDynamic()
        {
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            int chartId = Convert.ToInt32(Request.Form["chartId"]);
            service.Delete(chartId);

            return PartialView("/views/shared/userControls/SepetList.ascx", service.GetList(user.Id));
        }

        public ActionResult Delete(int id)
        {           
            int chartId = Convert.ToInt32(Request.Form["Chart" + id.ToString()]);
            service.Delete(chartId);
            
            return RedirectToAction("Index", "Sepet");
        }

        public ActionResult CompleteChart() {


            return View();

        }

        public ActionResult Cvc2()
        {
            return PartialView("/Views/Shared/UserControls/Cvc2.ascx");
        }

        public ActionResult OrderSuccess()
        {
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            var sepet = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];

            IList<SepetVm> sepetim = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            ChartUserControlVm chartInformation = new ChartUserControlVm();
            foreach (SepetVm urun in sepetim)
            {
                chartInformation.PartsTotal = chartInformation.PartsTotal + urun.Miktar;
                decimal? fiyat = 0;
                if (urun.UrunID.Fiyat != null)
                    fiyat = urun.UrunID.Fiyat;
                chartInformation.PriceTotal = chartInformation.PriceTotal + (fiyat ?? 0 * urun.Miktar);
            }

            chartInformation.Currency = "TL";
            
            ViewData["chart"] = chartInformation;
            UserService us = new UserService();
            
            return View(us.GetUser(user.Id));
        }

        public ActionResult ClearChart()
        {
            var user = (LoginUser)System.Web.HttpContext.Current.Session["loginUser"];
            IList<SepetVm> sepetim = (IList<SepetVm>)System.Web.HttpContext.Current.Session["chart"];
            service.ClearChart(sepetim);
            return PartialView("/views/shared/userControls/SepetList.ascx", service.GetList(user.Id));
        }

        #endregion
    }
}
